package array

func uniquePathsWithObstacles(obstacleGrid [][]int) int {
	m, n := len(obstacleGrid), len(obstacleGrid[0])
	dp := make([][]int, m)
	for i := 0; i < m; i++ {
		dp[i] = make([]int, n)
	}
	for i := range dp {
		for j := range dp[i] {
			if i == 0 || j == 0 {
				if i == 0 && j > 0 && dp[i][j-1] == 0 {
					dp[i][j] = 0
				} else if j == 0 && i > 0 && dp[i-1][j] == 0 {
					dp[i][j] = 0
				} else if obstacleGrid[i][j] == 1 {
					dp[i][j] = 0
				} else {
					dp[i][j] = 1
				}
				continue
			}

			if obstacleGrid[i][j] == 1 {
				dp[i][j] = 0
			} else {
				dp[i][j] = dp[i-1][j] + dp[i][j-1]
			}
		}
	}

	return dp[m-1][n-1]
}
